<?php
namespace App\Model;

use DB;

class NewActivityOrder{

	public $scool;

	public function __construct()
	{
		$this->scool = DB::connection('scool');
	}

	//获取所有 event
	public function getList($activityId, $isAdmin, $userId, $dataArr)
	{
        if($isAdmin){
            return $this->scool->table('new_activity_order')
                ->select('new_activity_order.*')
//                ->join('new_activity', 'new_activity.id', '=', 'new_activity_order.activity_id')
                ->where('new_activity_order.activity_id', $activityId)
//                ->where('new_activity.user_id', $userId)
                ->where('new_activity_order.status', $dataArr['status'])
                ->orderBy('new_activity_order.id' ,'desc')
                ->paginate(10);
        }else{
            return $this->scool->table('new_activity_order')
                ->select('new_activity_order.*')
                ->join('new_activity', 'new_activity.id', '=', 'new_activity_order.activity_id')
                ->where('new_activity_order.activity_id', $activityId)
                ->where('new_activity.user_id', $userId)
                ->where('new_activity_order.status', 1)
                ->orderBy('new_activity_order.id' ,'desc')
                ->paginate(10);
        }
	}

	//创建数据
	public function create($data)
	{
		$ret = DB::connection('scool')->table('new_activity_tmp')->insertGetId($data);
        return $ret;
	}

    /**
     * 创建活动
     * @param $data
     * @return mixed
     */
    public function createEvent($data)
    {
        $ret = DB::connection('scool')->table('new_activity')->insertGetId($data);
        return $ret;
    }

	//更新数据
	public function update($data, $id)
	{
		return $this->scool->table('new_activity')->where('id', $id)->update($data);
	}

    //更新数据
    public function updatetmp($data, $id)
    {
        return $this->scool->table('new_activity_tmp')->where('id', $id)->update($data);
    }


	//获取单条数据信息
	public function getInfoById($id, $userId)
	{
		return $this->scool->table('new_activity_order')
            ->select('new_activity_order.*')
            ->join('new_activity', 'new_activity.id', '=', 'new_activity_order.activity_id')
            ->where('new_activity_order.id', $id)
//            ->where('new_activity.user_id', $userId)
//            ->where('new_activity_order.status', 1)
            ->first();
	}

    //获取单条数据信息
    public function getInfoByIdTmp($id)
    {
        return $this->scool->table('new_activity_tmp')->where('id', $id)->first();
    }

    public function getTicketByEventId($id)
    {
        return $this->scool->table('new_activity_ticket')->where('object_id', $id)->get();
    }


	//删除记录
	public function delete($id)
	{
		$this->scool->table('all_list')->where('object_id', $id)->where('object_type', 1)->update(['status' => 0]);

		return $this->scool->table('info_share')->where('id', $id)->update(['status' => 0]);
	}

    //删除记录
    public function deletetmp($id)
    {
        return $this->scool->table('new_activity_tmp')->where('id', $id)->delete();
    }

	//文章中的多图
	public function getShareImagesById($id)
	{
		return $this->scool->table('info_share_img')->where('info_share_id', $id)->get();
	}

	//获取机器人
	public function getRobot()
	{
		$rs = $this->scool->table('user')->where('is_robot', 1)->where('is_black', 0)->select('id')->get();

		return $rs;
	}

	//机器人增加喜欢数
	public function robotLike($num, $id)
	{
		$robot = $this->getRobot();

		shuffle($robot);

		$useRobot = array_slice($robot, 0, $num);

		$like = 0;

		$insertArr = [];

		foreach($useRobot as $rel) {

			$rs = $this->scool->table('like')->where('object_id', $id)->where('user_id', $rel->id)->first();

			if(!$rs){

				$insertArr[] = [
								'object_id' 	=> $id, 
								'object_type' 	=> 1, 
								'user_id' 		=> $rel->id, 
								'create_time' 	=> time()
							];
			}

		}
		if($insertArr){
			$rs = $this->scool->table('like')->insert($insertArr);
		}
		
		$countLike = $this->scool->table('like')->where('object_id', $id)->count();

		$this->scool->table('info_share')->where('id', $id)->update(['like' => $countLike]);
	
		return $rs ? true : false;

	}

    /**
     * 写入 new_activity_ticket 表
     * @param $data
     * @return mixed
     */
    public function createTicket($data)
    {
        return DB::connection('scool')->table('new_activity_ticket')->insert($data);
    }

    /**
     * 更新数据
     * @param $data
     * @param $id
     * @return mixed
     */
    public function updateTicket($data, $id)
    {
        return $this->scool->table('new_activity_ticket')->where('object_id', $id)->where('is_member', $data['is_member'])->update($data);
    }

    /**
     * 更新 回退 理由
     * @param $data
     * @return mixed
     */
    public function updateReturnReason($data)
    {
        return $this->scool->table('new_activity_tmp')->where('id', $data['id'])->update(['return_reason' => $data['returnReason']]);
    }

}